﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GE_Management_Tool.Category;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using Gem_Tools_Utility;
using GE_Management_Tool;

namespace Gem_Tools.Category
{
    public partial class frmA002_Category_Main : frmBase, IRole
    {
        IList<CClient> m_lsClient;
        CClient m_objClient_Selected;
        IList<CStaff> m_lsStaff;
        CStaff m_objStaff_Selected;

        Boolean m_blAllowAdd = true;
        Boolean m_blAllowUpdate = true;
        Boolean m_blAllowDelete = true;

        DataTable m_dt = new DataTable();

        public frmA002_Category_Main()
        {
            InitializeComponent();

            m_dt.Columns.Add("Auto_ID");
            m_dt.Columns.Add("Regency");

            DataRow v_row = m_dt.NewRow();
            v_row["Auto_ID"] = 0;
            v_row["Regency"] = "Artist";
            m_dt.Rows.Add(v_row);

            v_row = m_dt.NewRow();
            v_row["Auto_ID"] = 1;
            v_row["Regency"] = "Technician";
            m_dt.Rows.Add(v_row);

            v_row = m_dt.NewRow();
            v_row["Auto_ID"] = 2;
            v_row["Regency"] = "Producer";
            m_dt.Rows.Add(v_row);

            v_row = m_dt.NewRow();
            v_row["Auto_ID"] = 3;
            v_row["Regency"] = "Admin";
            m_dt.Rows.Add(v_row);

            v_row = m_dt.NewRow();
            v_row["Auto_ID"] = 4;
            v_row["Regency"] = "Art Lead";
            m_dt.Rows.Add(v_row);

            repositoryItemLookUpEdit1.DataSource = m_dt;

            CCommon_Function.Format_All(this);
        }

        private void frmA002_Category_Main_Load(object sender, EventArgs e)
        {
            try
            {
                m_lsClient = new List<CClient>();
                CClient_Controller v_objCtrlClient = new CClient_Controller();
                m_lsClient = v_objCtrlClient.List_Client();

                m_lsStaff = new List<CStaff>();
                CStaff_Controller v_objCtrlSTaff = new CStaff_Controller();
                m_lsStaff = v_objCtrlSTaff.List_Staff();

                grdClient.DataSource = m_lsClient;
                gridControl1.DataSource = m_lsStaff;
                this.splitContainerControl1.Panel1.Width = (CConfig.g_intGem_Tools_Current_Width * 80) / 100;
                this.splitContainerControl2.Panel1.Width = (CConfig.g_intGem_Tools_Current_Width * 80) / 100;

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        //--------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------Client gridview function------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------

        private void grvClient_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objClient_Selected = (CClient)grvClient.GetRow(grvClient.FocusedRowHandle);

                //hiển thị thông tin lên giao diện:
                lbCountry.Text = m_objClient_Selected.Country;
                lbClientName.Text = m_objClient_Selected.Client_Name;
                txtContactsInfo.Text = m_objClient_Selected.Contacts_Info;
                txtNote.Text = m_objClient_Selected.Note;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnCreate_Client_Click(object sender, EventArgs e)
        {
            try
            {
                Allow_Add(m_blAllowAdd);
                frmA002_Client_Edit_Form v_frm = new frmA002_Client_Edit_Form();
                CClient v_objClient = v_frm.Show_To_Create();
                if (v_objClient != null)
                {
                    m_lsClient.Add(v_objClient);
                }
                grdClient.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnEdit_Client_Click(object sender, EventArgs e)
        {
            try
            {
                Allow_Update(m_blAllowUpdate);
                if (m_objClient_Selected != null && grvClient.DataRowCount > 0)
                {
                    frmA002_Client_Edit_Form v_frm = new frmA002_Client_Edit_Form();
                    CClient v_objClient = v_frm.Show_To_Edit(m_objClient_Selected);
                    m_lsClient[grvClient.FocusedRowHandle] = v_objClient;

                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDelete_Client_Click(object sender, EventArgs e)
        {
            try
            {
                Allow_Delete(m_blAllowDelete);
                CClient_Controller v_CtrlClient = new CClient_Controller();
                if (XtraMessageBox.Show("Are You Sure You Want To Delete This Row ?", "Warning", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    v_CtrlClient.Delete_Client(m_objClient_Selected.Auto_ID, CSession.Active_User_Name);
                    XtraMessageBox.Show("Delete Success !", "Warning", MessageBoxButtons.OK);
                    m_lsClient.Remove(m_objClient_Selected);
                }
                gridControl1.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void grvClient_DoubleClick(object sender, EventArgs e)
        {
            btnEdit_Client_Click(null, null);
        }
        
        //--------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------
        //----------------------------------------------Staff gridview function-----------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------

        private void btnCreate_Staff_Click(object sender, EventArgs e)
        {
            try 
            {
                Allow_Add(m_blAllowAdd);
                frmA002_Staff_Edit_Form v_frm = new frmA002_Staff_Edit_Form();
                CStaff v_objStaff = v_frm.Show_To_Create();
                if (v_objStaff != null)
                {
                    m_lsStaff.Add(v_objStaff);
                }
                gridControl1.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnEdit_Staff_Click(object sender, EventArgs e)
        {
            try 
            {
                Allow_Update(m_blAllowUpdate);
                Boolean is_Change = false;
                if (m_objStaff_Selected != null && gridView1.DataRowCount > 0)
                {
                    frmA002_Staff_Edit_Form v_frm = new frmA002_Staff_Edit_Form();
                    CStaff v_objStaff = v_frm.Show_To_Edit(m_objStaff_Selected, ref is_Change);
                    if (v_objStaff != null && is_Change == true)
                    {
                        m_lsStaff[gridView1.FocusedRowHandle] = v_objStaff;
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void btnDel_Staff_Click(object sender, EventArgs e)
        {
            try 
            {
                Allow_Delete(m_blAllowDelete);
                CStaff_Controller v_CtrlStaff = new CStaff_Controller();
                if (XtraMessageBox.Show("Are You Sure You Want To Delete This Row ?", "Warning", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    v_CtrlStaff.Delete_Staff(m_objStaff_Selected.Auto_ID, CSession.Active_User_Name);
                    XtraMessageBox.Show("Delete Success !", "Warning", MessageBoxButtons.OK);
                    m_lsStaff.Remove(m_objStaff_Selected);
                }
                gridControl1.RefreshDataSource();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnEdit_Staff_Click(null, null);
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                m_objStaff_Selected = (CStaff)gridView1.GetRow(gridView1.FocusedRowHandle);

                //Hiển thị thông tin lên giao diện:
                if (m_objStaff_Selected != null)
                {
                    lbFullName.Text = m_objStaff_Selected.Full_Name;
                    lbUserName.Text = m_objStaff_Selected.UserName;
                    lbEmail.Text = m_objStaff_Selected.Email;
                    lbExt.Text = m_objStaff_Selected.Extension.ToString();
                    lbRegency.Text = m_dt.Rows[m_objStaff_Selected.Regency_ID]["Regency"].ToString();
                    txtNote_Staff.Text = m_objStaff_Selected.Note;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        /// <summary>
        /// Phần này là phần phân quyền sau này tất cả các Form đều phải có các hàm này.
        /// </summary>
        /// <param name="pAllow_Add"></param>

        public void Allow_Add(bool pAllow_Add)
        {
            if (pAllow_Add == false)
                throw new Exception("Add Denied.");
        }

        public void Allow_Update(bool pAllow_Update)
        {
            if (pAllow_Update == false)
                throw new Exception("Edit Denied.");
        }

        public void Allow_Delete(bool pAllow_Delete)
        {
            if (pAllow_Delete == false)
                throw new Exception("Delete Denied.");
        }
    }
}